home
***
CD-ROM
|
disk
|
FTP
|
other
***
search
/
PC World Komputer 2010 April
/
PCWorld0410.iso
/
pluginy Firefox
/
11255
/
11255.xpi
/
chrome
/
content
/
controller
/
buttons
/
ButtonsHandler.js
< prev
next >
Wrap
Text File
|
2009-12-16
|
14KB
|
348 lines
/* ***** BEGIN LICENSE BLOCK *****
*
* Pearltrees add-on AMO, Copyright(C), 2009, Broceliand SAS, Paris, France
* (company in charge of developing Pearltrees)
*
* This file is part of ΓÇ£Pearltrees add-on AMOΓÇ¥.
*
* Pearltrees add-on AMO is free software: you can redistribute it and/or modify it under the
* terms of the GNU General Public License version 3 as published by the Free Software Foundation.
*
* Pearltrees add-on AMO is distributed in the hope that it will be useful, but WITHOUT ANY WARRANTY;
* without even the implied warranty of MERCHANTABILITY or FITNESS FOR A PARTICULAR PURPOSE.
* See the GNU General Public License for more details.
*
* You should have received a copy of the GNU General Public License along with Pearltrees add-on AMO.
* If not, see <http://www.gnu.org/licenses/>
*
* ***** END LICENSE BLOCK ***** */
// ///////////////////////////////////////////////////////////////////////////////
// Buttons controller
// ///////////////////////////////////////////////////////////////////////////////
var BRO_ButtonsHandler = {
DEFAULT_BAR_WIDTH : 350,
MODE_BUTTON_MAX_CHAR : 7,
_recordingModeInitValue : null,
restoreDefaultPostionOnLoad:false,
init : function(recordingMode) {
// Use init param because BRO_toolbar.getRecordingMode() is wrong after
// a call with BRO_tools.callWithDelay... (weird)
this._recordingModeInitValue = recordingMode;
window.addEventListener("load", BRO_ButtonsHandler.onViewLoad, false);
},
// @todo call this function when buttons come from the customize panel
onButtonsCreated : function() {
BRO_recordButtonController.fixButtonSize();
// Button effects are using CSS. So we sent the src to null.
var recordButton = document.getElementById("BRO_recordButton");
var newButton = document.getElementById("BRO_newButton");
var homeButton = document.getElementById("BRO_homeButton");
if (recordButton)
recordButton.image = null;
if (newButton)
newButton.image = null;
if (homeButton)
homeButton.image = null;
// backup selected mode from preferences
BRO_recordButtonController.refreshModeSelection(this._recordingModeInitValue);
BRO_inButtonController.initTreeList();
},
addPanels: function() {
if(BRO_toolbar.isBrowserVersionGreaterOrEqual("3.0")) {
var mainWindow = document.getElementById('main-window');
// Add createNewTree panel
var newTreePanel = document.createElement('panel');
newTreePanel.setAttribute('id', "BRO_newTreePanel");
newTreePanel.setAttribute('class', "BRO_menuPopup");
newTreePanel.setAttribute('onpopupshowing', "BRO_inButtonController.onNewTreePanelShowing();");
var newTreePanelContent = document.createElement('vbox');
newTreePanelContent.setAttribute('id', "BRO_newTreePanelContent");
newTreePanelContent.setAttribute('class', "BRO_nameMapPopupContent");
newTreePanelContent.setAttribute('onCancel', "BRO_inButtonController.onClickCancelNewTree()");
newTreePanelContent.setAttribute('onValidate', "BRO_inButtonController.onClickValidateNewTree()");
newTreePanel.appendChild(newTreePanelContent);
mainWindow.appendChild(newTreePanel);
// Add note panel
var notePanel = document.createElement('panel');
notePanel.setAttribute('id', "BRO_notePanel");
notePanel.setAttribute('class', "BRO_menuPopup");
notePanel.setAttribute('onpopupshowing', "BRO_noteController.onPanelShowing();");
var notePanelContent = document.createElement('vbox');
notePanelContent.setAttribute('id', "BRO_notePanelContent");
//notePanelContent.setAttribute('defaultText', "kikou");
notePanelContent.setAttribute('class', "BRO_notePopupContent");
notePanelContent.setAttribute('onCancel', "BRO_noteController.onClickCancelNote()");
notePanelContent.setAttribute('onValidate', "BRO_noteController.onClickValidateNote()");
notePanel.appendChild(notePanelContent);
mainWindow.appendChild(notePanel);
}
},
closeButtonPopups : function () {
BRO_inButtonController.closePopup();
BRO_recordButtonController.closePopup();
BRO_noteController.closePopup();
},
onViewLoad : function() {
var d = new Date();
var timeToLoad = d.getTime() - BRO_toolbar.initTime;
BRO_log.log("view loaded (" + timeToLoad + " ms)");
BRO_toolbar.viewLoaded = true;
if (BRO_toolbar.isFirstInstall) {
BRO_ButtonsHandler.addFirstInstallButtonInNavBar();
BRO_model.getTreesAndCurrentUser(skipNotificationIfNotLogged = true);
}
else if (BRO_toolbar.isUpdate) {
if(BRO_ButtonsHandler.restoreDefaultPostionOnLoad) {
BRO_ButtonsHandler.restoreDefaultPositionInNavbar();
BRO_ButtonsHandler.restoreDefaultPostionOnLoad = false;
}
BRO_model.getTreesAndCurrentUser(skipNotificationIfNotLogged = true);
}
BRO_ButtonsHandler.onButtonsCreated();
BRO_ButtonsHandler.addPanels();
var d = new Date();
var timeToLoad = d.getTime() - BRO_toolbar.initTime;
BRO_log.log("creation complete (" + timeToLoad + " ms)");
},
startRecording : function() {
// Update record button
BRO_recordButtonController.refreshRecordButtonLabel(BRO_toolbar.isRecording);
BRO_buttonEffectHelper.setButtonClass('BRO_recordButton','BRO_recordButtonIsRecording');
// Update new button
BRO_inButtonController.refreshTreeListTextHeader(isRecording = true);
BRO_inButtonController.refreshTreeListHeight();
},
stopRecording : function() {
// Update record button
BRO_recordButtonController.refreshRecordButtonLabel(BRO_toolbar.isRecording);
BRO_buttonEffectHelper.stopIsRecordingEffect();
// Update new button
BRO_inButtonController.refreshTreeListTextHeader(isRecording = false);
BRO_inButtonController.refreshTreeListHeight();
},
startOrContinueHistory : function(forceStartNew) {
BRO_toolbar.alertedForManyPearls = false;
// The current page can be recorded
if (!BRO_navListener.urlLoading && BRO_model.isValidUrl(BRO_browserManager.getSelectedBrowserUrl())) {
if (BRO_inButtonController.getSelectedTree() && !forceStartNew) {
this.recordCurrentPage();
} else if (BRO_inButtonController.getSelectedHistory() && !forceStartNew) {
this.recordCurrentPage();
} else {
this.recordCurrentPage(recordInNewTree = true);
}
BRO_toolbar.setRecording(true);
// Stop recording immediately if the mode is "one by one"
if (BRO_toolbar.recordingMode == RECORING_MODE_ONE_BY_ONE) {
BRO_tools.callWithDelay('BRO_toolbar.setRecording(false)', BRO_buttonEffectHelper.START_RECORDING_EFFECT_TIME);
}
}
// The current page can't be recorded, we start recording without saving
// the page
else {
if (BRO_inButtonController.getSelectedTree() && !forceStartNew) {
BRO_model.continueHistory(BRO_inButtonController.getSelectedTree().treeID);
} else if (BRO_inButtonController.getSelectedHistory() && !forceStartNew) {
BRO_model.continueHistory(null, BRO_inButtonController.getSelectedHistory().id);
} else {
BRO_model.start(BRO_inButtonController._selectedNewHistory.name);
}
BRO_actionListener.clear();
// Start recording only if the mode is "continuous"
if (BRO_toolbar.recordingMode == RECORING_MODE_CONTINUOUS) {
BRO_toolbar.setRecording(true);
}
}
},
/**
* Try to record the current page
*
* @param boolean isStartNew
*/
recordCurrentPage : function(isStartNew) {
var url = BRO_browserManager.getSelectedBrowserUrl();
var title = BRO_tools.removeFirefoxNameFromTitle(window.document.title);
var method = BRO_METHOD_UNKNOWN;
var browserID = BRO_browserManager.getSelectedBrowserID();
var time = BRO_tools.getTime();
var treeID = (BRO_inButtonController.getSelectedTree()) ? BRO_inButtonController.getSelectedTree().treeID : null;
var historyID = (BRO_inButtonController.getSelectedHistory()) ? BRO_inButtonController.getSelectedHistory().id : null;
var newHistoryName = (BRO_inButtonController._selectedNewHistory) ? BRO_inButtonController._selectedNewHistory.name : null;
// The current page can be recorded
if (!BRO_navListener.urlLoading && BRO_model.isValidUrl(url)) {
if (!BRO_toolbar.isCurrentTreeFull()) {
BRO_model.add(url, title, method, browserID, time, isStartNew, treeID, newHistoryName, historyID);
if(method != BRO_ButtonsHandler.BRO_METHOD_TAB_CREATED) {
BRO_toolbar.addUrlRecorded(url);
}
BRO_actionListener.clear();
}
}
// The current page can't be recorded and recording options are used, we
// start recording without saving the page
else if (isStartNew || treeID || historyID) {
this.startOrContinueHistory();
}
},
onClickToolbar : function() {
if (BRO_toolbar.helpStartupWindow) {
BRO_toolbar.helpStartupWindow.close();
}
},
addFirstInstallButtonInNavBar:function() {
var navbar = document.getElementById('nav-bar');
// Show navbar
if (navbar) {
navbar.collapsed = false;
}
this.appendButtonInNavbar("BRO_firstInstallButton", "urlbar-container");
// Make sure other buttons are in the customize palette
this.removeButton("BRO_homeButton");
this.removeButton("BRO_newButton");
this.removeButton("BRO_recordButton");
},
restoreDefaultPositionInNavbar : function() {
// First restore default recording mode
BRO_toolbar.setRecordingMode(DEFAULT_RECORDING_MODE);
var navbar = document.getElementById('nav-bar');
// Show navbar
if (navbar) {
navbar.collapsed = false;
}
// Remove the firstInstall button
this.removeButton("BRO_firstInstallButton");
// Install buttons
this.appendButtonInNavbar("BRO_homeButton", "urlbar-container");
this.appendButtonInNavbar("BRO_newButton", "BRO_homeButton");
this.appendButtonInNavbar("BRO_recordButton", "BRO_newButton");
},
removeAllButtons:function() {
this.removeButton("BRO_firstInstallButton");
this.removeButton("BRO_homeButton");
this.removeButton("BRO_newButton");
this.removeButton("BRO_recordButton");
},
removeButton : function(buttonId) {
var palette = document.getElementById("navigator-toolbox").palette;
var button = document.getElementById(buttonId);
if(!button) return;
var parentBar = button.parentNode;
while(button && parentBar) {
if(parentBar) {
this.removeButtonFromToolbarCurrentSet(parentBar, buttonId);
}
// Move item to the toolbar palette
if (palette) {
// @todo check the item is not already in the palette
palette.appendChild(button);
} else {
parentBar.removeChild(button);
}
BRO_log.log("Button removed: "+buttonId);
// If the button has been duplicated we remove other instances
button = document.getElementById(buttonId);
if(button) {
parentBar = button.parentNode;
}
}
},
appendButtonInNavbar : function(buttonId, beforeElementId) {
var toolbar = document.getElementById("nav-bar");
var button = document.getElementById(buttonId);
var beforeElement = document.getElementById(beforeElementId);
// Remove button if exist
if (button) {
this.removeButton(buttonId);
}
// Insert the button at the right position
toolbar.insertItem(buttonId, beforeElement);
this.appendButtonInToolbarCurrentSet(toolbar, buttonId, beforeElementId);
BRO_log.log("Button added: "+buttonId);
},
appendButtonInToolbarCurrentSet : function(toolbar, buttonId, beforeElementId) {
var oldset = toolbar.getAttribute("currentset");
if(!oldset) {
oldset = toolbar.getAttribute("defaultset");
}
if(oldset.indexOf(buttonId) != -1) {
this.removeButtonFromToolbarCurrentSet(toolbar, buttonId);
}
var newset = "";
if(!beforeElementId || oldset.indexOf(beforeElementId) == -1) {
if (oldset && oldset != "") {
newset = oldset + ",";
}
newset += buttonId;
}
else {
var beforeElementIndex = oldset.indexOf(beforeElementId);
var setBefore = oldset.substring(0, beforeElementIndex);
var setAfter = oldset.substring(beforeElementIndex, oldset.length);
newset = setBefore + buttonId + "," + setAfter;
}
toolbar.setAttribute("currentset", newset);
document.persist(toolbar.id, "currentset");
return newset;
},
removeButtonFromToolbarCurrentSet : function(toolbar, buttonId) {
var oldset = toolbar.getAttribute("currentset");
if (!oldset || oldset == "" || oldset.indexOf(buttonId) == -1)
return oldset;
var reg = new RegExp(buttonId + ",?", "gi");
var newset = oldset.replace(reg, "");
if (newset.charAt(newset.length - 1) == ",") {
newset = newset.substring(0, newset.length - 1);
}
toolbar.setAttribute("currentset", newset);
document.persist(toolbar.id, "currentset");
return newset;
}
}